<template>
	<view>
		<image class="login-bg" src="../../static/icons/login-bg.png"></image>
		<view class="content">
			<view class="card">
				<view class="font" >
					<image class="logo" src="../../static/icons/logo.jpg" mode="widthFix"></image>
					<view class="brand">华颉优站</view>
					<view  class="name">物联星链网运营管理平台-补液端</view>
					<view class="item u-f-ac">
						<image class="icon" src="../../static/icons/account.png" mode="widthFix"></image>
						<input class="input" type="text" v-model.trim="username" placeholder="输入登录账号" placeholder-class="pla-class" />
					</view>
					<view class="item u-f-ac">
						<image class="icon" src="../../static/icons/password.png" mode="widthFix"></image>
						<input class="input" type="text" password v-model.trim="password" placeholder="输入登录密码" placeholder-class="pla-class" />
					</view>
					<view class="checkbox-wrap">
						<checkbox-group @change="checkboxChange">
							<label class="u-f-ac">
								<checkbox style="transform: scale(0.8);" value="1" :checked="isSaveCode" color="#00a1f1" @change="checkboxChange" />
								记住密码
							</label>
						</checkbox-group>
					</view>
					<button v-show="bntIsActive" class="btn u-f-ajc active" open-type="getUserInfo" @getuserinfo="refillLogin">登录</button>
					<button v-show="!bntIsActive" class="btn u-f-ajc" type="default">登录</button>
					<div v-if="!isShowDev" class="radio-wrap" style="height: 100rpx;width: 100%;" @tap="fastclick"></div>
					<radio-group v-else @change="radioChangeHandle" class="radio-wrap">
						<label>
							<radio value="http://192.168.1.34:9666" />
							<text>http://192.168.1.47:9666</text>
						</label>
						<label>
							<radio value="https://api.etubang.com" />
							<text>https://api.etubang.com</text>
						</label>
					</radio-group>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			username: '',
			password: '',
			token: '123',
			isSaveCode: false,
			isShowDev: false,
			tapTimes: 0,
			isSharer: false
		};
	},
	computed: {
		bntIsActive() {
			return this.username !== '' && this.password !== '';
		}
	},
	onLoad() {
		const token = uni.getStorageSync('token');
		const lastloginInfo = uni.getStorageSync('lastloginInfo');
		if (lastloginInfo) {
			this.username = lastloginInfo.username;
			this.password = lastloginInfo.password;
			this.isSaveCode = true;
		}
		this.token = token;
		this.lastTime = new Date().getTime();
	},
	onShow() {
		this.getCode();
	},
	methods: {
		toggle() {
			this.isSharer = !this.isSharer;
		},
		fastclick() {
			// 连续点击5次以上 显示开发调试环境
			var currentTime = Date.now();
			// 计算两次相连的点击时间间隔
			this.tapTimes = currentTime - this.lastTime < 300 ? this.tapTimes + 1 : 1;
			this.lastTime = Date.now();
			clearTimeout(this.timer);
			if (this.tapTimes > 4) {
				// 连续点击五次或者五次以上的点击事件
				this.isShowDev = true;
			}
			this.timer = setTimeout(() => {
				// 处理点击事件
				console.log(this.tapTimes);
				this.tapTimes = 0;
			}, 300 + 10);
		},
		radioChangeHandle(e) {
			this.$u.http.setConfig({
				baseUrl: e.detail.value
			});
		},
		checkboxChange(e) {
			console.log(e);
			if (e.detail.value.length > 0) {
				this.isSaveCode = true;
			} else {
				this.isSaveCode = false;
			}
		},
		async refillLogin(e) {
			if(this.lock) return;
			const { encryptedData, iv, userInfo } = e.detail;
			const { username, password, code } = this;
			if (username == '' || password == '') {
				return uni.showToast({
					title: '请输入账号和密码',
					icon: 'none'
				});
			}
			await this.wxLogin()
			uni.showLoading();
			this.lock = true;
			this.$http({
				url: this.$api.authUrl + 'ma/oauth/token',
				data: { 
					username: username, 
					password: this.$encodeMD5(password), 
					code ,
					iv,
					encryptedData,
					tenantId: '000003',
					grant_type: 'password',
					scope: 'all',
					type: 'account',
					openId: this.openId,
				},
				loading: 0
			}).then(res => {
				// 是否记住密码
				if (this.isSaveCode) {
					uni.setStorageSync('lastloginInfo', { username, password });
				} else {
					uni.removeStorageSync('lastloginInfo');
				}
				uni.showToast({
					title: '登录成功'
				});
				this.$store.commit('login', res.data);
				setTimeout(() => {
					uni.reLaunch({
						url: '/pages/index/index'
					});
				}, 888);
				this.lock = false;	
			}).finally(()=>{
				this.lock = false;
			});
			this.getCode();
		},
		getCode() {
			uni.login({
				success: res => {
					this.code = res.code;
				}
			});
		},
		wxLogin() {
			return new Promise((resolve, reject) => {
				uni.login({
					provider: 'weixin',
					success: rs => {
						this.$http({
							url: this.$api.authUrl + 'ma/wx/token',
							data: {
								code: rs.code
							},
							loading: 0,
							hideLoading: 0
						}).then(res => {
		
							this.openId = res.data.openid
							resolve(res)
						}).catch(err => {
							reject(err)
						})
					}
				})
			})
		}
	}
};
</script>

<style lang="scss">
page {
	background: $color-primary;
}
.login-bg {
	width: 116vw;
	position: fixed;
	bottom: -160rpx;
	z-index: 100rpx;
	transform: translateX(-8vw);
}
.content {
	position: absolute;
	perspective: 2000px;
	
	.title {
		margin-top: 80rpx;
		margin-bottom: 60rpx;
		font-size: 32rpx;
		font-weight: bold;
	}
}

.logo {
	width: 120rpx;
	height: 120rpx;
	margin-top: 40rpx;
}

.brand {
	font-weight: bold;
	font-size: 50rpx;
	margin: 20rpx;
}

.name {
	margin-bottom: 60rpx;
	font-size: 36rpx;
}

.card {
	position: absolute;
	top: 80rpx;
	left: calc((100vw - 690rpx) / 2);
	width: 690rpx;
	display: flex;
	flex-direction: column;
	align-items: center;
	border-radius: 20rpx; 
	
	&.s1 {
		transform: rotateY(180deg);
	}
	
}

.font  {
	background-color: #FFFFFF;
	position: absolute;
	width: 690rpx;
	// height: 66vh;
	display: flex;
	flex-direction: column;
	align-items: center;
	box-shadow: 0rpx 0rpx 1rpx 2rpx #eee;
	border-radius: 20rpx;
	backface-visibility: hidden;
	
	.role {
		font-weight: 800;
		padding: 60rpx 0;
		font-size: 34rpx;
	}
	
	.btn-switch {
		position: absolute;
		right: 30rpx;
		top: 30rpx;
		color: #999999;
		text-decoration: underline;
		
		>image {
			width: 40rpx;
			height: 40rpx;
		}
	}
	
	.item {
		width: 600rpx;
		height: 84rpx;
		border-radius: 40rpx;
		background-color: #f0f0f0;
		margin-bottom: 30rpx;
		padding: 0 40rpx;
		position: relative;
		
		.icon {
			width: 30rpx;
			height: 33rpx;
		}
	}

	.btn {
		margin-top: 120rpx;
		color: rgba(95, 101, 112, 1);
		width: 600rpx;
		border-radius: 44rpx;
		height: 88rpx;
		&.active {
			background: $color-primary;
			color: #FFFFFF;
		}
	}
	.form-bg {
		position: absolute;
		bottom: 0;
		right: 0;
		width: 530rpx;
		height: 200rpx;
	}

	.input {
		flex: 1;
		margin: 0 20rpx;
		text-align: center;
		line-height: 84rpx;
		margin-left: -20rpx;
	}

	.checkbox-wrap {
		width: 100%;
		display: flex;
		justify-content: flex-start;
		margin-left: 100rpx;
		margin-top: 20rpx;
		font-size: 30rpx;
		color: #666;
	}
}

.pla-class {
	font-size: 28rpx;
	color: rgba(153, 153, 153, 1);
}

.radio-wrap {
	display: flex;
	flex-direction: column;
	margin-top: 30rpx;
	z-index: 9999;
	> label {
		margin-bottom: 20rpx;
	}
}
</style>
